package com.fht.service.statement.impl;

import com.fht.entity.StatementOrder;
import com.fht.from.StatementFrom;
import com.fht.mapper.statement.StatementOrderMapper;
import com.fht.service.statement.StatementOrderService;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StatementOrderServiceImpl implements StatementOrderService {

    @Resource
    public StatementOrderMapper statementOrderMapper;

    @Override
    public LayUITableVO<List<StatementOrder>> findPage(Map<String, Object> map) {
        StatementFrom from = (StatementFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(StatementOrder.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",from.getMerchantsId());

        if (! "".equals(from.getStatementId()) && null!=from.getStatementId()){
            c.andLike("id","%"+from.getStatementId()+"%");
        }

        if (! "".equals(from.getMeansExchange()) && null!=from.getMeansExchange()){
            c.andLike("meansExchange","%"+from.getMeansExchange()+"%");
        }

        if (! "".equals(from.getModePay()) && null!=from.getModePay()){
            c.andLike("modePay","%"+from.getModePay()+"%");
        }

        if (! "".equals(from.getCashier()) && null!=from.getCashier()){
            c.andLike("cashier","%"+from.getCashier()+"%");
        }

        if (! "".equals(from.getMemberId()) && null!=from.getMemberId()){
            c.andLike("memberId","%"+from.getMemberId()+"%");
        }

        List<StatementOrder> list = statementOrderMapper.selectByExample(example);

        for (StatementOrder statementOrder : list) {
            statementOrder.setCreated(UnitConversionUtil.conversionDate(statementOrder.getCreated()));
            statementOrder.setTotalString(UnitConversionUtil.conversion(statementOrder.getTotal()));
        }


        PageInfo<StatementOrder> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<StatementOrder>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }
}
